package com.example.testapp.activity.activity.meact;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.helper.AlbumHelper;
import com.example.testapp.activity.model.ImageBucket;
import com.example.testapp.activity.view.bitmap.BitmapCache;

import java.io.Serializable;
import java.util.List;

/**
 * Created by l on 2016/7/3.
 */
public class PictureActivity extends AppCompatActivity {

    private static final String PICTURE_ITEM = "picture_item";
    private static final String EXTRA_IMAGE_LIST = "imageList";
    private String picture;
    public static Bitmap bitmap;

    private GridView mGridView = null;
    private ImageBucketAdapter  mAdapter;

    private List<ImageBucket> dataList;
    private AlbumHelper  helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        picture = getIntent().getStringExtra(PICTURE_ITEM);
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        initData();
        initView();
    }

    private void initData() {
       dataList = helper.getImagesBucketList(false);
       bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_addpic_unfocused);
    }

    private void initView() {
       mGridView = (GridView)findViewById(R.id.gridview);
       mAdapter = new ImageBucketAdapter(this, dataList);
       mGridView.setAdapter(mAdapter);
       mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PictureActivity.this, ImageGridActivity.class);
                intent.putExtra(PictureActivity.EXTRA_IMAGE_LIST, (Serializable) dataList.get(position).imageList);
                startActivityForResult(intent, 100);
                finish();
           }
       });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != Activity.RESULT_OK){
            return;
        }
        switch (requestCode){
            case 100:
                setResult(Activity.RESULT_OK);
                finish();
                break;
            default:
                break;
        }
    }

    public class ImageBucketAdapter extends BaseAdapter{
        final String TAG = getClass().getSimpleName();

        private Activity act;
        private BitmapCache cache;
        private List<ImageBucket>  dataList;

        public ImageBucketAdapter(Activity activity, List<ImageBucket> List) {
           this.act = activity;
           dataList = List;
           cache = new BitmapCache();
        }

        BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
            @Override
            public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
                if(imageView != null && bitmap != null){
                    String url = (String) params[0];
                    if(url != null && url.equals(imageView.getTag())){
                        imageView.setImageBitmap(bitmap);
                    }else{
                        Log.e(TAG, "callback, bmp not match");
                    }
                }else{
                    Log.e(TAG, "callback, bmp null");
                }
            }
        };

        @Override
        public int getCount() {
            int count = 0;
            if(dataList != null){
                count = dataList.size();
            }
            return count;
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(act, R.layout.view_item_bucket, null);
                holder.mLoadIV = (ImageView) convertView.findViewById(R.id.loadIV);
                //holder.mSelectedIV = (ImageView) convertView.findViewById(R.id.selectIV);
                holder.mNameTV = (TextView) convertView.findViewById(R.id.nameTV);
                holder.mCountTV = (TextView) convertView.findViewById(R.id.countTV);
                convertView.setTag(holder);
            }else{
                 holder = (ViewHolder) convertView.getTag();
            }
            ImageBucket item = dataList.get(position);
            holder.mCountTV.setText("" + item.count);
            holder.mNameTV.setText(item.bucketName);
            //holder.mSelectedIV.setVisibility(View.GONE);
            if(item.imageList != null && item.imageList.size() > 0){
                String thumbPath = item.imageList.get(0).thumbnailPath;
                String sourcePath = item.imageList.get(0).imagePath;
                holder.mLoadIV.setTag(sourcePath);
                cache.displayBitmap(holder.mLoadIV, thumbPath, sourcePath, callback);
            }else{
                holder.mLoadIV.setImageBitmap(null);
                Log.e(TAG, "no images in bucket " + item.bucketName);
            }
            return convertView;
        }

        public class ViewHolder{
            ImageView mLoadIV;
            ImageView mSelectedIV;
            TextView mNameTV;
            TextView mCountTV;
        }
    }
}
